<?php
declare(strict_types=1);
use Yaf\Exception\TypeError;

/**
 * @method static set(string $name, mixed $value, ?int $expire = null): bool
 * @method static get(array|string $name): mixed
 * @method static inc(string $name, int $step = 1): bool|int
 * @method static dec(string $name, int $step = 1): bool|int
 * @method static delete(string $name): bool
 * @method static flush(): bool
 */
class Cache
{
    /**
     * @var string
     */
    private static string $className = "";

    /**
     * Handle dynamic, static calls to the object.
     *
     * @param string $method
     * @param array $args
     * @return mixed
     *
     * @throws RuntimeException|TypeError
     */
    public static function __callStatic(string $method, array $args)
    {
        $config = Config::get("cache");
        if (in_array($config['driver'], ["redis", "memcache", "memcached"])) {
            $driverClass = ucfirst($config['driver'] . "Storage");
        }else {
            $driverClass = ucfirst($config['driver']);
        }
        $prefix = $config['prefix'];
        if (!$prefix) $prefix = 'yaf_';
        $class = Cache::$className = ("Cache\Driver\\") . $driverClass;
        $instance = new $class($prefix);
        if (! $instance) {
            throw new RuntimeException(self::$className." is class not exist ");
        }
        return $instance->$method(...$args);
    }
}
